package com.today.step.lib.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.Default;
import com.litesuits.orm.db.annotation.NotNull;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
import com.today.step.lib.TodayStepDBHelper;

@Table(TodayStepDBHelper.TABLE_NAME)
public class TodayStepData implements Parcelable {
    // 指定自增，每个对象需要有一个主键
    @PrimaryKey(AssignType.AUTO_INCREMENT)
    private int id;

    @NotNull
    @Column(TodayStepDBHelper.COLUMN_DATE_YMD) //当天时间，只显示到天 yyyy-MM-dd
    private String today;

    @NotNull
    @Column(TodayStepDBHelper.COLUMN_DATE_DS) //步数时间，显示到毫秒
    private long date;

    @NotNull
    @Default("0")
    @Column(TodayStepDBHelper.COLUMN_STEPS)    //对应date时间的步数
    private long step;

    public TodayStepData() {
        id = -1;
        today = "";
        date = -1;
        step = -1;
    }

    public String getToday() {
        return today;
    }

    public void setToday(String today) {
        this.today = today;
    }

    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }

    public long getStep() {
        return step;
    }

    public void setStep(long step) {
        this.step = step;
    }

    public long getStepsByDateYMD(String dateYMD) {
        if (TextUtils.equals(today, dateYMD)) {
            return step;
        } else {
            return 0;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.today);
        dest.writeLong(this.date);
        dest.writeLong(this.step);
    }

    protected TodayStepData(Parcel in) {
        this.id = in.readInt();
        this.today = in.readString();
        this.date = in.readLong();
        this.step = in.readLong();
    }

    public static final Creator<TodayStepData> CREATOR = new Creator<TodayStepData>() {
        @Override
        public TodayStepData createFromParcel(Parcel source) {
            return new TodayStepData(source);
        }

        @Override
        public TodayStepData[] newArray(int size) {
            return new TodayStepData[size];
        }
    };

    @Override
    public String toString() {
        return "TodayStepData{" +
                "today='" + today + '\'' +
                ", date=" + date +
                ", step=" + step +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TodayStepData stepData = (TodayStepData) o;

        return today != null ? today.equals(stepData.today) : stepData.today == null;
    }

    @Override
    public int hashCode() {
        return today != null ? today.hashCode() : 0;
    }
}
